package com.example.androidexample.window.dialogs

import android.content.res.Resources
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.androidexample.R
import com.example.basic.window.dialog.CoustomDialog

class DialogActivity : AppCompatActivity(), SeekBar.OnSeekBarChangeListener,
    RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener {

    private lateinit var dialogView: View
    private lateinit var leftTopSeekbar: SeekBar
    private lateinit var rightTopSeekbar: SeekBar
    private lateinit var leftBottomSeekbar: SeekBar
    private lateinit var rightBottomSeekbar: SeekBar
    var lTmRadius = 0f
    var rTmRadius = 0f
    var lBmRadius = 0f
    var rBmRadius = 0f
    private lateinit var windowHeightSeekbar: SeekBar
    private lateinit var windowWidthSeekbar: SeekBar
    var windowHeight = -1
    var windowWidth = -1
    var gravity_rg: RadioGroup? = null
    var gravity = Gravity.CENTER
    private lateinit var window_x_seekbar: SeekBar
    private lateinit var window_y_seekbar: SeekBar
    var windowX = 0
    var windowY = 0
    private lateinit var is_trans_bg_cb: CheckBox
    var isBgTrasparent = true
    private lateinit var window_outsize_alpha: SeekBar
    var outsize_alpha = 0.0f
    private lateinit var anim_rg: RadioGroup
    var anim = 0
    private lateinit var mLibtTitle: TextView
    private lateinit var mLibtBack: TextView

    fun initOnCreate() {
        //弹窗四个边角
        leftTopSeekbar = findViewById(R.id.left_top_seekbar)
        rightTopSeekbar = findViewById(R.id.right_top_seekbar)
        leftBottomSeekbar = findViewById(R.id.left_bottom_seekbar)
        rightBottomSeekbar = findViewById(R.id.right_bottom_seekbar)
        leftTopSeekbar.setOnSeekBarChangeListener(this)
        rightTopSeekbar.setOnSeekBarChangeListener(this)
        leftBottomSeekbar.setOnSeekBarChangeListener(this)
        rightBottomSeekbar.setOnSeekBarChangeListener(this)

        //弹窗大小
        windowHeightSeekbar = findViewById(R.id.window_height_seekbar)
        windowWidthSeekbar = findViewById(R.id.window_width_seekbar)
        windowHeightSeekbar.setOnSeekBarChangeListener(this)
        windowWidthSeekbar.setOnSeekBarChangeListener(this)
        windowWidthSeekbar.max = resources.displayMetrics.widthPixels
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            windowWidthSeekbar.min = resources.displayMetrics.widthPixels / 2
        }

        //弹窗位置
        gravity_rg = findViewById(R.id.gravity_rg)
        gravity_rg!!.setOnCheckedChangeListener(this)

        //弹窗偏移
        window_x_seekbar = findViewById(R.id.window_x_seekbar)
        window_y_seekbar = findViewById(R.id.window_y_seekbar)
        window_x_seekbar.setOnSeekBarChangeListener(this)
        window_y_seekbar.setOnSeekBarChangeListener(this)

        //背景透明
        is_trans_bg_cb = findViewById(R.id.is_trans_bg_cb)
        is_trans_bg_cb.setOnCheckedChangeListener(this)

        //外区域透明度
        window_outsize_alpha = findViewById(R.id.window_outsize_alpha)
        window_outsize_alpha.setOnSeekBarChangeListener(this)

        //选择动画
        anim_rg = findViewById(R.id.anim_rg)
        anim_rg.setOnCheckedChangeListener(this)
        mLibtTitle = findViewById(R.id.tv_title)
        mLibtTitle.text = "弹窗功能测试"
        mLibtBack = findViewById(R.id.libt_back)
        mLibtBack.setOnClickListener { view: View? ->
            this.onBack(
                view
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dialog)
        initOnCreate()
    }

    private fun onBack(view: View?) {
        finish()
    }

    fun showDialog(view: View?) {
        dialogView = LayoutInflater.from(this).inflate(R.layout.base_dialog_simple, null, false)
        CoustomDialog.CustomDialogParams() //单位dp
            .contentView(dialogView)
            .setCorners(
                dpToPxFloat(lTmRadius),
                dpToPxFloat(rTmRadius),
                dpToPxFloat(lBmRadius),
                dpToPxFloat(rBmRadius)
            )
            .setStrokeColorAndWidth(dpToPxInt(2), Color.parseColor("#696969"))
            .setWindowSize(resources.displayMetrics.widthPixels, windowHeight)
            .setGravity(gravity)
            .setWindowTranslate(dpToPxInt(windowX), dpToPxInt(windowY))
            .setBgTrasparent(isBgTrasparent)
            .setBgOutSizeAlpha(outsize_alpha)
            .setAnimations(anim)
            .build(this)
            .show()
    }

    fun dpToPxInt(dp: Int): Int {
        return (dp * Resources.getSystem().displayMetrics.density + 0.5f).toInt()
    }

    fun dpToPxFloat(dp: Float): Float {
        return dp * Resources.getSystem().displayMetrics.density + 0.5f
    }

    override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
        val id = seekBar.id
        if (id == R.id.left_top_seekbar) {
            lTmRadius = progress * 1.0f
        } else if (id == R.id.right_top_seekbar) {
            rTmRadius = progress * 1.0f
        } else if (id == R.id.left_bottom_seekbar) {
            lBmRadius = progress * 1.0f
        } else if (id == R.id.right_bottom_seekbar) {
            rBmRadius = progress * 1.0f
            //弹窗大小
        } else if (id == R.id.window_height_seekbar) {
            windowHeight = progress
        } else if (id == R.id.window_width_seekbar) {
            windowWidth = progress
            //弹窗偏移
        } else if (id == R.id.window_x_seekbar) {
            windowX = progress - 150
        } else if (id == R.id.window_y_seekbar) {
            windowY = progress - 150
        } else if (id == R.id.window_outsize_alpha) {
            outsize_alpha = progress / 100.0f
        }
    }

    override fun onStartTrackingTouch(seekBar: SeekBar?) {}

    override fun onStopTrackingTouch(seekBar: SeekBar?) {}

    override fun onCheckedChanged(group: RadioGroup?, checkedId: Int) {
        if (checkedId == R.id.top_rb) {
            gravity = Gravity.TOP
        } else if (checkedId == R.id.bottom_rb) {
            gravity = Gravity.BOTTOM
        } else if (checkedId == R.id.left_rb) {
            gravity = Gravity.LEFT
        } else if (checkedId == R.id.right_rb) {
            gravity = Gravity.RIGHT
        } else if (checkedId == R.id.center_rb) {
            gravity = Gravity.CENTER
        } else if (checkedId == R.id.none_rb) {
            anim = 0
        } else if (checkedId == R.id.transition_rb) {
            anim = R.style.DialogTransAnim
        } else if (checkedId == R.id.alpha_rb) {
            anim = R.style.DialogAlphaAnim
        } else if (checkedId == R.id.scale_rb) {
            anim = R.style.DialogScaleAnim
        } else if (checkedId == R.id.rotate_rb) {
            anim = R.style.DialogRotateAnim
        }
    }

    override fun onCheckedChanged(buttonView: CompoundButton?, isChecked: Boolean) {
        isBgTrasparent = isChecked
    }
}